﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
//using DatabaseManager;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;

namespace prjQlyKinhDoanh
{
    public class ReportUtility
    {
        public static void LoadReportFile(string rptFilePath, CrystalDecisions.Windows.Forms.CrystalReportViewer rptViewer, string sStoreName, IDataParameter[] parameters, string sTableName)
        {
            if (!System.IO.File.Exists(rptFilePath))
            {
                throw (new Exception(" Đường dẫn file: " + rptFilePath + " không đúng. Hãy kiểm tra lại!"));
            }
            else
            {
                DBConnect obj = new DBConnect();
                DataSet ds = obj.returnDataSet(sStoreName, sTableName, parameters);
                //DataSet ds = obj.returnDataSet(sStoreName, parameters);                
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                rptDocument.Load(rptFilePath);
                rptDocument.SetDataSource(ds.Tables[sTableName]);

                ParameterFields paramFields = new ParameterFields();
                ParameterField paramField;
                ParameterDiscreteValue discreteValue;
                foreach (SqlParameter para in parameters)
                {
                    paramField = new ParameterField();
                    paramField.Name = para.ParameterName.ToString();
                    discreteValue = new ParameterDiscreteValue();
                    discreteValue.Value = para.Value;
                    paramField.CurrentValues.Add(discreteValue);
                    paramFields.Add(paramField);
                }
                rptViewer.ShowCloseButton = false;
                rptViewer.ShowRefreshButton = false;
                rptViewer.ShowGroupTreeButton = false;
                rptViewer.ReportSource = rptDocument;
                rptViewer.ParameterFieldInfo = paramFields;
            }
        }

        public static void LoadReportFile(string rptFilePath, DataSet ds, CrystalDecisions.Windows.Forms.CrystalReportViewer rptViewer)
        {
            if (!System.IO.File.Exists(rptFilePath))
            {
                //MessageBox.Show(new Exception(" Đường dẫn file: " + rptFilePath + " không đúng. Hãy kiểm tra lại!"),Declare.titleError,MessageBoxButtons.OK,MessageBoxIcon.Error);
                throw (new Exception(" Đường dẫn file: " + rptFilePath + " không đúng. Hãy kiểm tra lại!"));
            }
            else
            {
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                rptDocument.Load(rptFilePath);
                rptDocument.SetDataSource(ds);
                rptViewer.ShowCloseButton = false;
                rptViewer.ShowRefreshButton = false;
                rptViewer.ShowGroupTreeButton = false;
                rptViewer.DisplayGroupTree = false;
                rptViewer.DisplayToolbar = true;
                rptViewer.ReportSource = rptDocument;
            }
        }

        public static void LoadReportFile(string rptFilePath, string sql, CrystalDecisions.Windows.Forms.CrystalReportViewer rptViewer, string[] ParaName, string[] ParaValue)
        {
            if (!System.IO.File.Exists(rptFilePath))
            {
                throw (new Exception(" Đường dẫn file: " + rptFilePath + " không đúng. Hãy kiểm tra lại!"));
            }
            else
            {
                SqlConnection objConn = new SqlConnection(DBConnect.GetConnectionString());
                SqlDataAdapter da = new SqlDataAdapter(sql, objConn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                rptDocument.Load(rptFilePath);
                rptDocument.SetDataSource(ds.Tables[0]);

                ParameterFields paramFields = new ParameterFields();
                ParameterField paramField;
                ParameterDiscreteValue discreteValue;
                for (int i = 0; i <= ParaName.Length - 1; i++)
                {
                    paramField = new ParameterField();
                    paramField.Name = ParaName[i];
                    discreteValue = new ParameterDiscreteValue();
                    discreteValue.Value = ParaValue[i];
                    paramField.CurrentValues.Add(discreteValue);
                    paramFields.Add(paramField);

                }
                rptViewer.ShowCloseButton = false;
                rptViewer.ShowRefreshButton = false;
                rptViewer.ShowGroupTreeButton = false;
                rptViewer.ReportSource = rptDocument;
                rptViewer.ParameterFieldInfo = paramFields;
            }
        }

        public static void LoadReportFile(string rptFilePath, CrystalDecisions.Windows.Forms.CrystalReportViewer rptViewer, string[] ParaName, object[] ParaValue)
        {
            if (!System.IO.File.Exists(rptFilePath))
            {
                throw (new Exception(" Đường dẫn file: " + rptFilePath + " không đúng. Hãy kiểm tra lại!"));
            }
            else
            {
                try
                {
                    CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                    reportDocument.Load(rptFilePath);

                    ConnectionInfo crConnectionInfo = new ConnectionInfo();
                    crConnectionInfo.AllowCustomConnection = true;
                    crConnectionInfo.ServerName = System.Configuration.ConfigurationSettings.AppSettings.Get("ServerName");
                    crConnectionInfo.DatabaseName = System.Configuration.ConfigurationSettings.AppSettings.Get("Database");
                    crConnectionInfo.UserID = System.Configuration.ConfigurationSettings.AppSettings.Get("User");
                    crConnectionInfo.Password = System.Configuration.ConfigurationSettings.AppSettings.Get("Password");
                    reportDocument.SetDatabaseLogon(crConnectionInfo.UserID, crConnectionInfo.Password, crConnectionInfo.ServerName, crConnectionInfo.DatabaseName);
                    AssignConnectionInfo(reportDocument, crConnectionInfo);
                    TableLogOnInfos tableLogOnInfos = new TableLogOnInfos();
                    TableLogOnInfo tbl = new TableLogOnInfo();
                    tbl.ConnectionInfo = crConnectionInfo;
                    tableLogOnInfos.Add(tbl);
                    rptViewer.LogOnInfo = tableLogOnInfos;

                    rptViewer.ParameterFieldInfo = GetParameterFields(ParaName, ParaValue);
                    rptViewer.ReportSource = reportDocument;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        public static void PrintReportFile(string rptFilePath, CrystalDecisions.Windows.Forms.CrystalReportViewer rptViewer, string[] ParaName, object[] ParaValue)
        {
            if (!System.IO.File.Exists(rptFilePath))
            {
                throw (new Exception(" Đường dẫn file: " + rptFilePath + " không đúng. Hãy kiểm tra lại!"));
            }
            else
            {
                ParameterFields paramFields = new ParameterFields();
                ParameterField paramField;
                ParameterDiscreteValue discreteValue;
                for (int i = 0; i <= ParaName.Length - 1; i++)
                {
                    paramField = new ParameterField();
                    paramField.Name = ParaName[i];
                    discreteValue = new ParameterDiscreteValue();
                    discreteValue.Value = ParaValue[i];
                    paramField.CurrentValues.Add(discreteValue);
                    paramFields.Add(paramField);
                }
                CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                rptViewer.ParameterFieldInfo = paramFields;
                reportDocument.Load(rptFilePath);
                reportDocument.SetDatabaseLogon("sa", "", @".\SQLEXPRESS", "CMND", false);
                rptViewer.ReportSource = reportDocument;
                rptViewer.PrintReport();
            }
        }

        public static ParameterFields GetParameterFields(string[] ParaName, object[] ParaValue)
        {
            ParameterFields paramFields = new ParameterFields();
            ParameterField paramField;
            ParameterDiscreteValue discreteValue;
            for (int i = 0; i <= ParaName.Length - 1; i++)
            {
                paramField = new ParameterField();
                paramField.Name = ParaName[i];
                discreteValue = new ParameterDiscreteValue();
                discreteValue.Value = ParaValue[i];
                paramField.CurrentValues.Add(discreteValue);
                paramFields.Add(paramField);
            }
            return paramFields;
        }

        private static void AssignConnectionInfo(ReportDocument document, ConnectionInfo crConnection)
        {
            try
            {
                foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
                {
                    TableLogOnInfo logOnInfo = table.LogOnInfo;
                    if (logOnInfo != null)
                    {
                        logOnInfo.ConnectionInfo = crConnection;
                        table.ApplyLogOnInfo(logOnInfo);
                        table.Location = logOnInfo.ConnectionInfo.DatabaseName + ".dbo." + table.Location.Substring(table.Location.LastIndexOf(".") + 1);
                    }
                }
            }
            catch{}
        }

    }
}
